
package com.oocl.chat.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.chat.controller.Client;
import com.oocl.chat.pojo.Message;

public class RegisterFrame extends JFrame implements ActionListener {
	private JTextField nameTf;
	private JPasswordField  psdPf;
	private JButton RegisterBtn;
	private JButton cancelBtn;
	private String username;
	public RegisterFrame() {
		this.setTitle("欢迎注册");
		this.setSize(400, 260);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		init();
		addEvent();
		this.setVisible(true);
	}
	private void init(){
		this.setLayout(null);
		nameTf=new JTextField();
		psdPf=new JPasswordField();
		RegisterBtn=new JButton("注册");
		cancelBtn=new JButton("cancel");
		nameTf.setBounds(50, 40, 300, 40);
		psdPf.setBounds(50, 90, 300, 40);
		RegisterBtn.setBounds(80, 150, 90, 40);
		cancelBtn.setBounds(220, 150, 90, 40);
		this.add(nameTf);
		this.add(psdPf);
		this.add(RegisterBtn);
		this.add(cancelBtn);
	}
	private void addEvent(){
		RegisterBtn.addActionListener(this);
		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) {
		new RegisterFrame();
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==RegisterBtn){
			Date date = new Date();
			String username = nameTf.getText();
			String password = psdPf.getText();
			if("".equals(username)||"".equals(password)){
				JOptionPane.showMessageDialog(this, "提示消息", "用户名或密码没输入！！",JOptionPane.WARNING_MESSAGE);
				return;
			}
			this.setUsername(username);
			Message message = new Message();
			message.setSender(username);
			message.setDate(date.getTime());
			message.setContent(username+","+password);
			message.setType(6);
			Client.client.sendMessage(message);
		}
	}
	public void alertFormat(){
		JOptionPane.showMessageDialog(this, "提示消息", "用户名或密码没输入！！",JOptionPane.WARNING_MESSAGE);
	}
	public void alertErro(){
		JOptionPane.showMessageDialog(this, "提示消息", "用户名已存在",JOptionPane.WARNING_MESSAGE);  
	}
	public void alertErroInfo(){
		JOptionPane.showMessageDialog(this, "提示消息", "用户名或密码错误",JOptionPane.WARNING_MESSAGE);  
	}
	public void alertTrue(){
		JOptionPane.showMessageDialog(this, "提示消息", "注册成功！请登录",JOptionPane.WARNING_MESSAGE);
	}
}





